package com.gang.config;

import com.gang.listener.PollingMessageListener;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.jms.connection.SingleConnectionFactory;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

//@Configuration
//@Import(ActiveMqConfig.class)
public class ActiveMqListenerConfig {


    /**
     * 队列目的地
     *
     * @return 队列目的地
     */
    @Bean(name = "orderQueryQueueDestination")
    public ActiveMQTopic orderQueryQueueDestination() {
        return new ActiveMQTopic("MyTopic");
    }

    /**
     * 消息监听容器
     * @param singleConnectionFactory    连接工厂
     * @param orderQueryQueueDestination 消息目的地
     * @param pollingMessageListener     监听器实现
     * @return 消息监听容器
     */
    @Bean
    public DefaultMessageListenerContainer orderQueryQueueMessageListenerContainer(
         SingleConnectionFactory singleConnectionFactory,
        @Qualifier("orderQueryQueueDestination") ActiveMQTopic orderQueryQueueDestination,
        @Qualifier("pollingMessageListener") PollingMessageListener pollingMessageListener) {
        DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer();
        messageListenerContainer.setConnectionFactory(singleConnectionFactory);
        messageListenerContainer.setDestination(orderQueryQueueDestination);
        messageListenerContainer.setMessageListener(pollingMessageListener);
//        messageListenerContainer.setConcurrency();
        return messageListenerContainer;
    }
}
